package comits.business.tiers.rp.tasks.ws;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@SuppressWarnings("static-access")
public class Dates {
	
	 private Calendar date;

	 public Calendar getDate() {
		return date;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}
		
	public Dates()
	{
		this.date = Calendar.getInstance();
	}
		
	public String getMonth(){
	   	//System.out.println((date.get(this.date.MONTH)+1));
		//System.out.println(date.get(this.date.MONTH));
	   	return moisLettre(date.get(this.date.MONTH));
	}
	    
	public int getIntMonth(){
	  	//System.out.println((date.get(this.date.MONTH)+1));
	   	return date.get(this.date.MONTH);
	}
	    
	public int getYear(){
	   	//System.out.println((date.get(this.date.MONTH)+1));
	  	return this.getDate().YEAR;
	}
	    
	    
//   recuperation du mois en lettre		
		public String moisLettre(int mois){
			String mL ="";
			switch (mois)
			{
				case 0: mL="Janvier"; break;
				case 1: mL="Fevrier"; break;
				case 2: mL="Mars"; break;
				case 3: mL="Avril"; break;
				case 4: mL="Mai"; break;
				case 5: mL="Juin"; break;
				case 6: mL="Juillet"; break;
				case 7: mL="Aoet"; break;
				case 8: mL="Septembre"; break;
				case 9: mL="Octobre"; break;
				case 10: mL="Novembre"; break;
				case 11: mL="Decembre"; break;
			}
			return mL;
		}
	
	public Date setDat(int y,int m,int d){
		Calendar calendar1 = new GregorianCalendar();
        calendar1.set(Calendar.YEAR, y);
        calendar1.set(Calendar.MONTH, m);
        calendar1.set(Calendar.DAY_OF_MONTH, d);
            
        return calendar1.getTime();
	}
		
	@SuppressWarnings("deprecation")
	public int jourOuvres(Date date1, Date date2){
		int jO = 1;
		Calendar c = new GregorianCalendar();
		c = Calendar.getInstance();
        // Difference
        long diff = Math.abs(date2.getTime() - date1.getTime());
            long numberOfDay = (long)diff/86400000;
            c.set(date1.getYear(), date1.getMonth(), date1.getDay());
           // System.out.println(numberOfDay);
        	while(numberOfDay != 0){
        		//si date incremente different de dimanche on decremente la periode 
                    if(c.get(Calendar.DAY_OF_WEEK)!= Calendar.SUNDAY) //&& calendar1.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
                    	jO++;
                 // date du jour + 1
    				c.add(c.DATE, 1);
                    numberOfDay--;
           }
          //  System.out.println("Nombre de jours ouvres : " + jO);
            return jO;
		}
	
		public void getDateLivraison(int perioDelai) {
			// TODO Auto-generated method stub
			// prendre la date du jour plus periodicite et delai
			// pour recup la date de livraison j'increment la date du jour et decrement le delai +periodicite sauf quand la date = dimanche
			Calendar c = new GregorianCalendar();
			c = Calendar.getInstance();
			while(perioDelai != 0){
				// date du jour + 1
				c.add(c.DATE, 1);
				//si date incremente different de dimanche on decremente la periode 
                    if(c.get(Calendar.DAY_OF_WEEK)!= Calendar.SUNDAY) perioDelai--; //&& calendar1.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
           }
			this.setDate(c);
       //    System.out.println("date livraison : " + this.getDate().getTime());
		}

		@Override
		public String toString() {
			return "Dates [date=" + date.getTime() + "]";
		}

		public int getNbJOMois() {
			// TODO Auto-generated method stub
			int mois = getIntMonth();
			int an = getYear();
			Calendar c = new GregorianCalendar();
			c = Calendar.getInstance();
			c.set(Calendar.MONTH,mois);
			int jMax = c.getActualMaximum(Calendar.DAY_OF_MONTH);
			int jMin = c.getActualMinimum(Calendar.DAY_OF_MONTH);
			
			Date date1 = setDat(an,mois,jMin);
			Date date2 = setDat(an,mois,jMax);
			//System.out.println(date1);
			//System.out.println(date2);
			//System.out.println(jourOuvres(date1,date2));
			return jourOuvres(date1,date2);
		}
		
		public int getNbJoursInPeriod(int perioDelai,int mois) {
			// TODO Auto-generated method stub
			// prendre la date du jour plus periodicite et delai
			// pour recup la date de livraison j'increment la date du jour et decrement le delai +periodicite sauf quand la date = dimanche
			int nbJ = 0;
			// date du jour + 1
			//this.getDate().add(this.getDate().DATE, -1);
			while(perioDelai != 0){
				// date du jour + 1
				this.getDate().add(this.getDate().DATE, 1);
				//si date incremente different de dimanche on decremente la periode 
                    if (this.getDate().get(Calendar.DAY_OF_WEEK)!= Calendar.SUNDAY) {
                    	perioDelai--;
                    	if(this.getIntMonth()==mois) nbJ ++;
                    }
           }
       //    System.out.println("date livraison : " + this.getDate().getTime());
			return nbJ;
		}
		
		
}
